// server operator
// gets the players added to a game, creating a new game if necessary

package sbcc.edu.blackjack.server;

import java.util.ArrayList;

import sbcc.edu.blackjack.Player;
import sbcc.edu.blackjack.command.Command;

public class ServerOperator {
	private ArrayList<Player> connectedPlayer;
	private ArrayList<TableOperator> tables;
	
	public ServerOperator(){
		connectedPlayer = new ArrayList<Player>();
		tables = new ArrayList<TableOperator>();
	}
	
	public void addPlayer(Player noob){
		connectedPlayer.add(noob);
	}
	
	public void commandReceived(Command received){
		received.execute();
	}
	
	public void addPlayerToTable(Player p){
		if(tables.isEmpty()){
			TableOperator temp = new TableOperator();
			tables.add(temp);
			temp.addPlayer(p);
			connectedPlayer.add(p);
			new Thread(temp).start();
		} else {		
			for(TableOperator t : tables){
				if (t.isTableFull() == false){
					t.addPlayer(p);
					connectedPlayer.add(p);
					break;
				}
			}
		}
	}
	
	public void removePlayerFromTable(Player p){
		int id = p.getTableId();
		for (TableOperator t : tables){
			if(t.getId() == id){
				t.removePlayer(p);
			}
		}
	}
}
